#include "EventLoop.h"
#include "EventDispatcher.h"

int EventDispatcher::dispatch_loop(EventLoop *loop, struct timeval *timeval) const {
	std::string thread_name = loop->threadName_;


	EpollDispatcherData *data = loop->eventDispatcherData_;

	int n = epoll_wait(data->efd, data->events, MAXEVENTS, -1);
	int fd;

	for (int i = 0; i < n; ++i) {
		if ((data->events[i].events & EPOLLERR)
			|| (data->events[i].events & EPOLLHUP)) {
			close(data->events[i].data.fd);
			continue;
		}


		fd = data->events[i].data.fd;
		if (data->events[i].events & EPOLLIN) {
			printf("get message channel fd == %d, for read, %s\n", data->events[i].data.fd, thread_name.c_str());			
			loop->channelEventActivate(fd, EVENT_READ);


		}

		if (data->events[i].events & EPOLLOUT) {
			printf("get message channel fd == %d, for write, %s\n", data->events[i].data.fd, thread_name.c_str());
			loop->channelEventActivate(fd, EVENT_WRITE);

		}
	}
	return 0;
}